﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using Microsoft.Web.UI.WebControls;
using UDS.Components;

namespace UDS.Inc
{
    /// <summary>
    ///	Lists all the features that the current user can access.
    /// </summary>
    [CLSCompliant(false)]
    public partial class ClassTreeView : UserControl
    {
        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var dt = InitRootNodeDataTable(SiteContext.Current.UserName);
                InitTreeRootNode(projTreeView.Nodes, dt);
                projTreeView.ExpandLevel = 1;
            }

            base.OnLoad(e);
        }

        /// <summary>
        /// 初始化 RootNode DataTable
        /// </summary>
        private static DataTable InitRootNodeDataTable(string username)
        {
            var data = new Database();
            var prams = new[] {
                Database.MakeInParam("@UserName", SqlDbType.NVarChar, 20, username)
            };

            using (SqlDataReader dataReader = data.RunDataReader("dbo.sproc_GetShowClass_V2", prams))
            {
                var dataTbl1 = Tools.ConvertDataReaderToDataTable(dataReader);
                dataTbl1.TableName = "TreeView";

                return dataTbl1;
            }
        }

        /// <summary>
        /// 初始化 ChildNode DataTable
        /// </summary>
        private static DataTable InitChildNodeDataTable(int ClassParentID)
        {
            var data = new Database();
            var prams = new[] {
                Database.MakeInParam("@Class_id", SqlDbType.Int, 20, ClassParentID)
            };

            using (SqlDataReader dataReader = data.RunDataReader("dbo.sproc_GetAllChildClass_V2", prams))
            {
                var dt = Tools.ConvertDataReaderToDataTable(dataReader);
                dt.TableName = "TreeView";

                return dt;
            }
        }

        /// <summary>
        /// 初始化TreeView 的 RootNode
        /// </summary>
        private void InitTreeRootNode(TreeNodeCollection root, DataTable dataTable)
        {
            DataView dataView = dataTable.Copy().DefaultView;

            //	dataView.RowFilter = "ClassParentID = ClassID";
            foreach (DataRowView drv in dataView)
            {
                int classId = (int)drv["ClassID"];
                bool disabledFeature = !UDSConfig.IsFeatureEnabled(classId);
                if (disabledFeature && UDSConfig.HideDisabledFeatures)
                {
                    continue;
                }

                var node = new TreeNode();
                node.ID = classId.ToString();
                node.Text = "<span onmousemove=javascript:title='" + drv["ClassName"] + "'>" + drv["ClassName"] + "</span>";
                node.ImageUrl = GetIcon(drv["ClassType"].ToString());
                if (!disabledFeature) 
                {
                    // The feature is enabled.
                    node.NavigateUrl = "Switch.aspx?Action=1&ClassID=" + drv["ClassID"];
                    node.Target = "MainFrame";
                }
                else
                {
                    // The feature is disabled.
                    node.Text = node.Text + " (暂无)";
                    node.NavigateUrl = "javascript:void(0);";
                    node.Target = "";
                }
                root.Add(node);

                var dt = InitChildNodeDataTable(classId);
                InitTreeChildNode(node.Nodes, classId, dt);
            }
        }

        /// <summary>
        /// 初始化TreeView 的 ChildNode
        /// </summary>
        private void InitTreeChildNode(TreeNodeCollection parentNodes, int classParentID, DataTable dataTable)
        {
            DataView dataView = dataTable.Copy().DefaultView;
            dataView.RowFilter = "ClassParentID = " + classParentID + "";

            foreach (DataRowView drv in dataView)
            {
                var id = (int)drv["ClassID"];
                var name = (string)drv["ClassName"];

                var node = new TreeNode();
                node.ID = id.ToString();
                node.Text = "<span onmousemove=javascript:title='" + HttpUtility.HtmlAttributeEncode(name) + "'>" + HttpUtility.HtmlEncode(name) + "</span>";
                node.ImageUrl = GetIcon(drv["ClassType"].ToString());
                node.NavigateUrl = "Switch.aspx?Action=1&ClassID=" + id;
                node.Target = "MainFrame";

                parentNodes.Add(node);
                InitTreeChildNode(node.Nodes, id, dataTable);
            }
        }

        #region 获取节点图标

        /// <summary>
        /// 获取节点图标
        /// </summary>
        private static string GetIcon(string ClassType)
        {
            string rtnValue = "../../DataImages/";
            switch (ClassType)
            {
                case "0":
                    rtnValue += "flag.gif";
                    break;
                case "1":
                    rtnValue += "myDoc.gif";
                    break;
                case "2":
                    rtnValue += "mail.gif";
                    break;
                case "3":
                    rtnValue += "page.gif";
                    break;
                case "4":
                    rtnValue += "staff.gif";
                    break;
                case "5":
                    rtnValue += "help_page.gif";
                    break;
                case "6":
                    rtnValue += "MyTask.gif";
                    break;
                case "7":
                    rtnValue += "mail.gif";
                    break;
                case "8":
                    rtnValue += "myDoc.gif";
                    break;
                case "9":
                    rtnValue += "DocFlow.gif";
                    break;
                case "10":
                    rtnValue += "ClientManage.gif";
                    break;
                case "11":
                    rtnValue += "myLinkman.gif";
                    break;
                case "12":
                    rtnValue += "position.gif";
                    break;
                case "13":
                    rtnValue += "roleManage.gif";
                    break;
                case "14":
                    rtnValue += "kaoqin.gif";
                    break;
                case "15":
                    rtnValue += "workadmin.gif";
                    break;
                case "16":
                    rtnValue += "message.gif";
                    break;
                case "17":
                    rtnValue += "usbkey.gif";
                    break;
                default:
                    rtnValue += "red_ball.gif";
                    break;
            }
            return rtnValue;
        }

        #endregion
    }
}